import pool_util from "./pool_util";
import a_star from "./a_star";
import GameController from "./game_controller";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

var CellBgG = cc.Class({
    name:'CellBgG',
    properties: {
        name:'',
        initPollCount:0,
        prefab:cc.Prefab,
        height:70,
        width:80
    }
});


@ccclass
export default class CellBgGroup extends cc.Component {

  
    @property({type:CellBgG})
    cellBgG=[];

   

    cellBgCol=5;
    cellBgRow=8;


    borderWidth=20;

  

    cellBgs;
    cellMap;

 

 



    // LIFE-CYCLE CALLBACKS:

     onLoad () {
        pool_util.batchInitObjPool(this,this.cellBgG);
        this.cellBgs= new Array<Array<any>>();
        this.cellMap=new Array<Array<any>>();
     }

   

    

    getRandomFreeCellBgs(count):cc.Node[]{
        let nodes:any=[];
        let cellBgFreeArr:any=[];
        for(let mapCol=0;mapCol<this.cellMap.length;mapCol++){
            for(let mapRow=0;mapRow<this.cellMap[0].length;mapRow++){
      
                if(this.cellMap[mapCol][mapRow]===0){
                    cellBgFreeArr.push(this.cellBgs[mapCol][mapRow]);
                }
            }
        }
 
        for(let i=0;i<count;i++){
            let index=Math.floor(Math.random()*cellBgFreeArr.length);
            nodes.push(cellBgFreeArr.splice(index,1)[0]);
        }

        return nodes;
    }

 

    
    createCellBg(){
            var poolName = this.cellBgG[0].name + 'Pool';
            let gridH=this.node.getParent().height;
            let gridW=this.node.getParent().width;

            let cellBgSpH=this.cellBgG[0].height+this.borderWidth;
            let cellBgSpW=this.cellBgG[0].width;
 
            let oddHeight=gridH-((this.cellBgRow)*cellBgSpH);
            let oddWidth=gridW-((this.cellBgCol)*cellBgSpW);
            oddHeight= oddHeight/2;
            oddWidth=oddWidth/2;
 
 
            for(let col=0;col<this.cellBgCol;col++){
                this.cellBgs[col]=[];
                this.cellMap[col]=[];
                for(let row=0;row<this.cellBgRow;row++){
                    let startY=oddHeight+this.cellBgG[0].height/2;
                    let startX=oddWidth;
                    if((col+1)%2===0){
                        startY-=cellBgSpH/2-this.borderWidth/2;
                    }
                    var newNode = pool_util.genNewNode(this[poolName],this.cellBgG[0].prefab,this.node);
                    newNode.getComponent("cell_bg").colRowPos=cc.v2(col,row);
                    this.cellBgs[col][row]=newNode;
                    this.cellMap[col][row]=0;

                    let pos=cc.v2(startX+(cellBgSpW*col)+this.cellBgG[0].width/2,startY+cellBgSpH*row);
                    newNode.setPosition(pos);
                    
                }
            }

        
    }

  

    // update (dt) {}
}
